package com.huiminpay.transaction.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class IDCardRecognition {
    public static void main(String[] args) {
        String host = "https://idcardocr.market.alicloudapi.com";
        String path = "/idcard";
        String method = "POST";
        String appcode = "your_appcode"; // 替换为自己的AppCode
        String idCardSide = "front"; // 身份证正反面，可选值：front、back
//        String imageUrl = "https://img.alicdn.com/imgextra/i3/2200727118195/O1CN01JzjJZB1QJZJZJZJZJ_!!2200727118195.jpg"; // 身份证图片链接
        String imageUrl = "https://huiminpay-longlong.oss-cn-beijing.aliyuncs.com/2fd65e9ce6b84dc7952d66953f0a4310.png"; // 身份证图片链接

        try {
            URL url = new URL(host + path + "?idCardSide=" + idCardSide + "&imageUrl=" + URLEncoder.encode(imageUrl, "UTF-8"));
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(method);
            connection.setRequestProperty("Authorization", "APPCODE " + appcode);
            connection.setDoOutput(true);

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            System.out.println(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}